# -*- coding:utf-8 -*-

from django.contrib import admin
from models import UniteFormation, OrganisationUf, DomaineFormation, ActiviteUf, Inspecteur, AttributionInspecteur
#import locale
# Locale pour les documents imprimés...
# locale.setlocale(locale.LC_TIME, "fr_BE.UTF-8")

class OrganisationUfInline(admin.TabularInline):
    model = OrganisationUf
    extra = 0
    fields = ['annee_scolaire', 'num_org', 'date_debut', 'date_fin']
    ordering = ('annee_scolaire', 'num_org',)
    readonly_fields = ['annee_scolaire', 'num_org', 'date_debut', 'date_fin']
    list_filter = ['annee_scolaire']

class ActiviteUfInline(admin.TabularInline):
    model = ActiviteUf
    extra = 0
    fields = ['numero', 'categorie', 'libelle', 'periodes']
    ordering = ('numero',)
    readonly_fields = ['numero', 'categorie', 'libelle', 'periodes']
    
    
class UniteFormationAdmin(admin.ModelAdmin):
    list_display = ('num_adm', 'libelle', 'doc8link', 'niveau', 'periodes', 'app_doc_8', 'inspecteur',)
    list_filter = ('niveau',)
    date_hierarchy = "app_doc_8"
    search_fields = ('libelle','num_adm')
    
    readonly_fields = ('num_adm', 'libelle', 'niveau', 'code', 'app_doc_8', 'niveau', 'periodes', 'ap_de', 'ap_dd', 'ad_de', 'ad_dd', 'inspecteur')
    
    fieldsets = (
        (None, 
            {'fields': (('libelle', 'num_adm',), 'libelle_doc_8', ('code', 'app_doc_8', 'niveau', 'periodes',), 'inspecteur', 'doc8',)}),
        ("Ouverture", 
            {'classes' : ('collapse',), 'fields' : ('doc8bis', 'app_8bis', 'num_8bis', 'transmis')}),
        ("Admission subventions",
            {'classes' : ('collapse',), 'fields' : ('ap_de', 'ap_dd', 'ad_de', 'ad_dd')}),
        ("Domaine de formation",
            {'classes' : ('collapse',), 'fields' : ('domaine',)}),
    )

    def formfield_for_dbfield(self, db_field, **kwargs):
        field = super(UniteFormationAdmin, self).formfield_for_dbfield(db_field, **kwargs)
        if db_field.name == 'libelle' or db_field.name == 'libelle_doc_8' :
            del field.widget.attrs['class']
            field.widget.attrs['size'] = '100'
        return field
    inlines = [ActiviteUfInline, OrganisationUfInline]
    class Media:
        css = { "all" : ("css/hide_admin_original.css",) }


class OrganisationUfAdmin(admin.ModelAdmin):
    list_display = ('uf_link', 'annee_scolaire', 'num_org', 'date_debut', 'date_fin',)
    list_display_links = ('date_debut',)
    list_filter = ['annee_scolaire']
    date_hierarchy = 'date_debut'
    ordering = ('annee_scolaire', 'num_org')

class AttributionInspecteurAdmin(admin.ModelAdmin):
    list_display = ["prefixe", "niveau", "inspecteur"]
    list_editable = ["niveau", "inspecteur"]
    list_display_links= ["prefixe",]
    ordering = ["prefixe",]

        
admin.site.register(UniteFormation, UniteFormationAdmin)
admin.site.register(OrganisationUf, OrganisationUfAdmin)
admin.site.register(DomaineFormation)
admin.site.register(Inspecteur)
admin.site.register(AttributionInspecteur, AttributionInspecteurAdmin)